﻿using System.Collections.Generic;
using Landpy.CruiseControlNet.DomainLayer.SerializerModule;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;
using Landpy.CruiseControlNet.FacadeInterface.SerializerModule;
using Landpy.CruiseControlNet.Infrastructure.Test;
using NUnit.Framework;
using System;
namespace Landpy.CruiseControlNet.ConfigurationSidekick.TestSuite.ConfigurationModule
{
    [TestFixture]
    public class SerializerUnitTest
    {
        private ISerializer<CruiseControl> Serializer { get; set; }
        private string SerializedFileName { get; set; }

        [TestFixtureSetUp]
        public void SetUp()
        {
            this.Serializer = new Serializer<CruiseControl>();
            this.SerializedFileName = TF.GetConfig().Properties["SerializedFileName"];
        }

        [TestFixtureTearDown]
        public void TearDown()
        {
        }

        [TestCase]
        public void TestSerializeAndDeserialize()
        {
            #region Generate Configuration Instance

            var cruiseControl = new CruiseControl()
            {
                InternalSecurity = new InternalSecurity()
                {
                    Users = new List<SecurityUser>()
                                               {
                                                   new LdapUser()
                                                       {
                                                           Name = "User",
                                                           Domain = "Domain"
                                                       },
                                                   new PasswordUser()
                                                        {
                                                            Name = "User",
                                                            Password = "Password",
                                                            Display = "UserDisplayName"
                                                        },
                                                    new UserName()
                                                        {
                                                            Name = "landpyuser"
                                                        }
                                               },
                    Permissions = new List<Permission>()
                                                                {
                                                                     new RolePermission()
                                                                         {
                                                                             DefaultRight=PermissionCategoryType.Inherit,
                                                                             Name = "RolePermission",
                                                                             UserNames = new List<UserName>()
                                                                                             {
                                                                                                 new UserName()
                                                                                                     {
                                                                                                         Name = "landpy1"
                                                                                                     },
                                                                                                 new UserName()
                                                                                                     {
                                                                                                         Name = "landpy2"
                                                                                                     }
                                                                                             }
                                                                         },
                                                                     new UserPermission()
                                                                         {
                                                                             ChangeProject = PermissionCategoryType.Allow,
                                                                             User = "landpy3"
                                                                         }
                                                                }
                },
                Projects = new List<Project>(){
                                new Project()
                                {
                                    Name = "Foundation1",
                                    Security = new Security()
                                                   {
                                                       Type= SecurityType.DefaultProjectSecurity,
                                                       Permissions=new List<Permission>()
                                                                       {
                                                                            new RolePermission()
                                                                            {
                                                                                DefaultRight=PermissionCategoryType.Inherit,
                                                                                Name = "RolePermission",
                                                                                UserNames = new List<UserName>()
                                                                                             {
                                                                                                 new UserName()
                                                                                                     {
                                                                                                         Name = "landpy1"
                                                                                                     },
                                                                                                 new UserName()
                                                                                                     {
                                                                                                         Name = "landpy2"
                                                                                                     }
                                                                                             }
                                                                            },
                                                                            new UserPermission()
                                                                            {
                                                                                ChangeProject = PermissionCategoryType.Allow,
                                                                                User = "landpy3"
                                                                            }
                                                                       }
                                                   },
                                    SourceControl = new SourceControl()
                                                        {
                                                            Type = SourceControlType.VSTS,
                                                            Server = @"http://landpy:8080/tfs/defaultcollection",
                                                            Project = @"$\Landpy SharePoint Foundation\Source\2010",
                                                            Domain = @"Landpy",
                                                            UserName = @"landpy",
                                                            Password = @"password",
                                                            WorkingDirectory = @"C:\Program Files (x86)\CruiseControl.NET\server\LandpyFoundation\WorkingDirectory",
                                                            DeleteWorkspace = Boolean.TrueString,
                                                            AutoGetSource = Boolean.FalseString,
                                                            CleanCopy = Boolean.TrueString,
                                                            Codepage = 936,
                                                            Workspace = "LandpyWorkSpace"
                                                        },
                                    Triggers = new List<Trigger>()
                                                   {
                                                       new IntervalTrigger()
                                                           {
                                                               Name = "continuous"
                                                           }
                                                   },
                                    Tasks = new List<Task>()
                                                {
                                                    new MSBuild()
                                                        {
                                                            Executable = @"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe",
                                                            ProjectFile = @"C:\Program Files (x86)\CruiseControl.NET\server\LandpyFoundation\WorkingDirectory\Landpy.Foundation.sln",
                                                            BuildArgs = @"/p:Configuration=Debug /l:FileLogger,Microsoft.Build.Engine;logfile=ccnet.log",
                                                            Logger = @"C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MSBuild.dll",
                                                            Timeout = 5000
                                                        },
                                                    new FacadeInterface.ConfigurationModule.NUnit()
                                                        {
                                                            Path = @"C:\Program Files (x86)\NUnit 2.5.10\bin\net-2.0\nunit-console.exe",
                                                            Assemblies = new List<Assembly>()
                                                                             {
                                                                                 new Assembly()
                                                                                     {
                                                                                         Text = @"C:\Program Files (x86)\CruiseControl.NET\server\Foundation\WorkingDirectory\Landpy.Core.Test\bin\Debug\Landpy.Core.Test.dll"
                                                                                     }
                                                                             }
                                                        },
                                                    new MSBuild()
                                                        {
                                                            Executable = @"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe",
                                                            ProjectFile = @"C:\Program Files (x86)\CruiseControl.NET\server\Foundation\WorkingDirectory\Landpy.Foundation\Landpy.Foundation.csproj",
                                                            BuildArgs = @"/p:Configuration=Debug /l:FileLogger,Microsoft.Build.Engine;logfile=ccnet.log",
                                                            Logger = @"C:\Program Files (x86)\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MSBuild.dll",
                                                            Timeout = 5000
                                                        },
                                                    new Exec()
                                                        {
                                                            Executable = @"C:\CIServer\CIServerWorkspace\FoundationBat\Process.bat"
                                                        }
                                                },
                                    Publishers = new List<Publisher>()
                                                     {
                                                         new XmlLogger(),
                                                         new Email()
                                                             {
                                                                 Mailport = 25,
                                                                 IncludeDetails = @"TRUE",
                                                                 MailhostUsername = @"Landpy\User",
                                                                 MailhostPassword = @"Password",
                                                                 UseSSL = @"FALSE",
                                                                 From = @"ciserver@mail.xxxx",
                                                                 Mailhost = @"MailServer.xxxx.local",
                                                                 Users = new List<User>()
                                                                             {
                                                                                 new User()
                                                                                     {
                                                                                         Name = @"landpyuser",
                                                                                         Address = @"landpyuser@mail.xxxx",
                                                                                         Group = @"landpygroup"
                                                                                     }
                                                                             },
                                                                Groups = new List<Group>()
                                                                             {
                                                                                 new Group()
                                                                                     {
                                                                                         Name = @"landpygroup",
                                                                                         Notifications = new List<NotificationType>()
                                                                                                             {
                                                                                                                 NotificationType.Change,
                                                                                                                 NotificationType.Exception,
                                                                                                                 NotificationType.Failed,
                                                                                                                 NotificationType.Fixed,
                                                                                                                 NotificationType.Success
                                                                                                             }
                                                                                     }
                                                                             },
                                                                ModifierNotificationTypes = new List<NotificationType>()
                                                                                              {
                                                                                                  NotificationType.Always
                                                                                              },
                                                                SubjectSettings = new List<Subject>()
                                                                                      {
                                                                                          new Subject()
                                                                                              {
                                                                                                  BuildResultType = BuildResultType.Exception,
                                                                                                  Value = @"[${CCNetProject}] occur exception, please check the TFS."
                                                                                              },
                                                                                          new Subject()
                                                                                              {
                                                                                                  BuildResultType = BuildResultType.Success,
                                                                                                  Value = @"[${CCNetProject}] build successfully!"
                                                                                              }
                                                                                      },
                                                                XslFiles = new List<XslFile>()
                                                                               {
                                                                                   new XslFile()
                                                                                       {
                                                                                           Text = @"xsl\header.xsl"
                                                                                       },
                                                                                   new XslFile()
                                                                                       {
                                                                                           Text = @"xsl\compile.xsl"
                                                                                       },
                                                                                   new XslFile()
                                                                                       {
                                                                                           Text = @"xsl\unittests.xsl"
                                                                                       },
                                                                                   new XslFile()
                                                                                       {
                                                                                           Text = @"xsl\modifications.xsl"
                                                                                       },
                                                                                   new XslFile()
                                                                                       {
                                                                                           Text = @"xsl\compile-msbuild.xsl"
                                                                                       }
                                                                               }
                                                             },
                                                        new ArtifactCleanup()
                                                            {
                                                                CleanUpMethodType = CleanUpMethodType.KeepLastXBuilds,
                                                                CleanUpValue = 5
                                                            }
                                                     }
                                },
                                new Project()
                                {
                                    Name = "Foundation2",
                                    SourceControl = new SourceControl()
                                                        {
                                                            Type = SourceControlType.SVN,
                                                            TrunkUrl = @"svn://svnserver/",
                                                        },
                                    Triggers = new List<Trigger>()
                                                   {
                                                       new CronTrigger()
                                                           {
                                                               CronExpression="* * 1 1 *"
                                                           }
                                                   }
                                }
                    }
            };

            #endregion

            string serializedString = this.Serializer.Serialize(cruiseControl);
            Assert.IsNotEmpty(serializedString);
            var deserializedCruiseControl = this.Serializer.DeSerialize(serializedString);
            Assert.AreEqual("Foundation1", deserializedCruiseControl.Projects[0].Name);
            Assert.AreEqual("http://landpy:8080/tfs/defaultcollection", cruiseControl.Projects[0].SourceControl.Server);
            Assert.AreEqual("Foundation2", deserializedCruiseControl.Projects[1].Name);
            Assert.AreEqual("svn://svnserver/", cruiseControl.Projects[1].SourceControl.TrunkUrl);
            Assert.AreEqual(SourceControlType.VSTS, cruiseControl.Projects[0].SourceControl.Type);
            Assert.AreEqual(SourceControlType.SVN, cruiseControl.Projects[1].SourceControl.Type);
            Assert.AreEqual("continuous", ((IntervalTrigger)cruiseControl.Projects[0].Triggers[0]).Name);
            Assert.AreEqual("* * 1 1 *", ((CronTrigger)cruiseControl.Projects[1].Triggers[0]).CronExpression);
        }

        [TestCase]
        public void TestDeserializeFile()
        {
            CruiseControl cruiseControl = this.Serializer.DeSerializeFile(this.SerializedFileName);
            Assert.IsNotNull(cruiseControl);
        }
    }
}
